package com.ttsx.core.common.result;

import lombok.Data;

/**
 * 返回结果类
 *
 * @author wb
 * @since 2025-03-01
 */

@Data
public class Result<T> {

    private Integer code;
    private String message;
    private T data;

    public static <T> Result<T> success() {
        return build(null, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> failure() {
        return build(null, ResultCodeEnum.FAILURE);
    }

    public static <T> Result<T> failure(ResultCodeEnum resultCodeEnum) {
        return build(null, resultCodeEnum);
    }


    public Result<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }


    private static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();

        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        result.setData(data);

        return result;
    }
}
